/**
 * @name  fromDate
 * @param {type} 返回类型 【 y-m-d h:i:s | y-m-d | ym | d 】
 * @param {time} 时间戳
 */
function fromDate(type, time) {
  let date = new Date(time * 1000)
  let Y = date.getFullYear() // 获取完整的年份(4位,1970)
  let M = date.getMonth() + 1 // 获取月份(0-11,0代表1月,用的时候记得加上1)
  let D = date.getDate() // 获取日(1-31)
  let H = date.getHours() // 获取小时数(0-23)
  let I = date.getMinutes() // 获取小时数(0-23)
  let S = date.getSeconds() // 获取秒数(0-59)
  let datetime = ''
  switch (type) {
    case 'y-m-d':
      datetime = Y + '-' + M + '-' + D
      break
    case 'y-m-d h:i:s':
      datetime = Y + '-' + M + '-' + D + ' ' + H + ':' + I + ':' + S
      break
    case 'd':
      datetime = D
      break
    case 'ym':
      datetime = Y + '-' + M
      break
    case 'i:s':
      if (I < 10) {
        I = '0' + I
      }
      if (S < 10) {
        S = '0' + S
      }
      datetime = I + ':' + S
      break
  }
  return datetime
}

function ArrayExist(arr) {
  let result = []
  let res = []
  for (let index = 0; index < arr.length; index++) {
    result[arr[index]['name']] = arr[index]
  }
  let i = 0
  for (var key in result) {
    res[i] = result[key]
    i++
  }
  return arr
}

function exist(arrList, arr) {
  for (var key in arrList) {
    if (arrList[key]['name'] == arr['name']) {
      return key
    }
  }
  return false
}

// 歌词分析
function lrcSplit(lrc) {
  var lyrics = lrc.split('\n') //this.lrc代表歌词文件内容的引用
  var lrcObj = {}
  for (var i = 0; i < lyrics.length; i++) {
    var lyric = decodeURIComponent(lyrics[i])
    var timeReg = /\[\d*:\d*((\.|\:)\d*)*\]/g
    var timeRegExpArr = lyric.match(timeReg)
    if (!timeRegExpArr) continue
    var clause = lyric.replace(timeReg, '')
    for (var k = 0, h = timeRegExpArr.length; k < h; k++) {
      var t = timeRegExpArr[k]
      var min = Number(String(t.match(/\[\d*/i)).slice(1)),
        sec = Number(String(t.match(/\:\d*/i)).slice(1))
      var time = min * 60 + sec
      lrcObj[time] = clause
    }
  }
  return lrcObj
}

export default {
  fromDate,
  ArrayExist,
  exist,
  lrcSplit
}
